//
//  PSFirstC.swift
//  CJPack
//
//  Created by 陈警卫 on 2023-08-14.
//

import SwiftUI

class PFirstC: CJBaseBackController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if #available(iOS 16.0, *) {
            var psfc = PSFirstC()
            psfc.color = .blue
            psfc.textStr = "SwiftUIView"
            
            let vc = UIHostingController(rootView: psfc)
            vc.view.frame = CGRect(x: 0.0, y: CJScreen.navBatBarHeight,
                                   width: self.view.cj_width,
                                   height: self.view.cj_height - CJScreen.navBatBarHeight)
            vc.view.backgroundColor = .clear
            self.view.addSubview(vc.view)
        }
    }
}


@available(iOS 16.0, *)
struct PSFirstC: View {
    // 测试
    var textStr: String = ""
    var color: Color = .red
    // 数据
    //@State var datas: [CJMFE.CJMFirstEntryData] = []

    var body: some View {
        VStack {
            List {
                /**
                 ForEach(datas, id: \.id) { model in
                 PSFirstV(name: model.name!)
                 .listRowBackground(Color.clear)
                 }
                 .listRowSeparator(.hidden)
                 */
            }
            .listStyle(.plain)
            .refreshable {
                print("refreshable")
            }
            Spacer()
        }
        .onAppear {
            Task {
                //let config = await PHttper().aw_firstEntry()
                //datas = config.model as! [CJHFE]
                /**
                 PHttper().firstEntry { config in
                 datas = config.model as! [CJHFE]
                 }                 
                 */
            }
        }
    }
}

struct PSFirstC_Previews: PreviewProvider {
    static var previews: some View {
        if #available(iOS 16.0, *) {
            //let model = CJMFE.CJMFirstEntryData(id: 0, name: "name", type: "1")
            //PSFirstC(datas: [model])
        }
    }
}


struct PSFirstV: View {
    
    var name: String
    
    var body: some View {
        
        HStack {
            Image(systemName: "seal.fill")
                .foregroundColor(.red)
                .font(.body)
            Text(name)
                .font(.headline)
            Spacer()
        }
        .padding()
    }
}
struct PSFirstV_Previews: PreviewProvider {
    static var previews: some View {
        PSFirstV(name: "name")
    }
}

/**
 @State var isRefresh = false
 @State var isMore = false
 
 @State var textArr : Array<String> = []
 @State var count = 20
 
 var body: some View {
     VStack {
         /*
          offDown: 列表数据滑动总高
          listH: 列表高度
          refreshing: 下拉刷新加载UI的开关
          isMore: 加载更多UI的开关
          */
         RefreshScrollView(offDown: CGFloat(textArr.count) * 40.0, listH: ScreenH - kNavHeight - kBottomSafeHeight, refreshing: $isRefresh, isMore: $isMore) {
             // 下拉刷新触发
             DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(3), execute: {
                 // 刷新完成，关闭刷新
                 self.loadData()
                 isRefresh = false
             })
         } moreTrigger: {
             // 上拉加载更多触发
             DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(3), execute: {
                 // 加载完成，关闭加载
                 for i in 0...10{
                     textArr.append(String("\(i + textArr.count) Hello, world!"))
                 }
                 isMore = false
             })
         } content: {
             // 列表内容
             VStack(spacing: 0){
                 ForEach(0..<(textArr.count),id: \.self) { index in
                     VStack{
                         Text(textArr[index] ).foregroundColor(Color.red).frame(width: 200, height: 40)
                     }
                 }
             }
         }

         Spacer()
     }.onAppear(){
         self.loadData()
     }
     .padding()
 }
 
 func loadData(){
     textArr.removeAll()
     for i in 0...count{
         textArr.append(String("\(i) Hello, world!"))
     }
 }
 */
